# 测试央企数据
import yaml
import pandas as pd

from datetime import datetime
from WindPy import w

from pyecharts.charts import Tab, Line
from plot_base import multi_grid_line, multi_macro_bar_or_line, item_timeline_seasonal, xy_match_same_freq_plot
from data_process import to_daily, to_monthly_mean, TjdSingleData, gen_xy_same_freq, cal_tztd_index

from pyecharts import options as opts
now = datetime.now().strftime("%Y-%m-%d")


def set_data_begin_time(df, meta):
    filter_date = max([meta[y_name][1] for y_name in df.columns])
    df = df[df.index > filter_date]
    return df


def get_data_by_meta(meta):
    code_to_name = {v[0]:k for k,v in meta.items()} # 代码与中文名映射
    edb_str = ','.join([s[0] for k,s in meta.items()]) # 长字符串
    early_dates = [s[1] for k,s in meta.items()] # 最早日期

    if not w.isconnected():
        w.start()    
    data = w.edb(edb_str, min(early_dates), now) # 一次性下载所有数据
    
    if data.ErrorCode == 0:
        df = pd.DataFrame(data.Data, 
                          index=data.Codes, 
                          columns=data.Times).T
        df.columns = [code_to_name[s] for s in df.columns]
    else:
        print(f"错误码 >> {data.ErrorCode}")
        df = pd.DataFrame()
    return df
    

def load_yaml_file(file_path):
    print(file_path)
    with open(file_path, 'r', encoding='utf-8') as file:
        return yaml.safe_load(file)

def combine_excel_df(excel_data,x_name ,df_macro, y_name, x_process_string):
    df_hf = TjdSingleData(excel_data[x_name])
    if len(x_process_string) < 2:
        df_x = df_hf.data
    else:
        if '_' in x_process_string:
            df_x = df_hf.get_long_string_data(x_process_string).data
        else:
            df_x = df_hf[x_process_string].data
    if '同比' in x_process_string:
        df_x = df_x * 100
    df_y = df_macro[y_name].dropna()
    df_y.index = pd.to_datetime(df_y.index)
    df_x.index = pd.to_datetime(df_x.index)
    # 截短数据
    first_date = max(df_x.index[0], df_y.index[0])
    df_y = df_y[first_date:]
    df_x = df_x[first_date:]
    # 合并数据
    df_cot = pd.concat([df_y, df_x], axis=1)
    df_cot = df_cot.round(1)
    return df_cot


meta = load_yaml_file('config.yaml')
df_macro = get_data_by_meta(meta['meta'])
compare_meta = load_yaml_file('compare.yaml')

names = ['GDP不变价当季同比', 
         'GDP工业不变价当季同比',
         'GDP交通运输不变价当季同比',
         'GDP信息传输不变价当季同比',
         'GDP北京累计同比',
         ]

tx_data = pd.read_excel('raw_data\油气数据.xlsx', index_col=0)
tx_data.index = pd.to_datetime(tx_data.index)

for x_name in tx_data.columns:
    print(x_name)
    tab = Tab(page_title=f"{x_name}相关性分析")
    x_process = '当季均值_当季同比'
    for y_name in names:
        df_xy = combine_excel_df(tx_data, x_name, df_macro, y_name,
                                    x_process)
        idx_fill, tztd_ratio = cal_tztd_index(df_xy)
        b = xy_match_same_freq_plot(df_xy, idx_fill, tztd_ratio)

        tab.add(b, y_name)
    tab.render(f"plot_yqsj\{x_name}.html")